﻿using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Controllers
{
    [AllowAnonymous]

    public class YongHuZhongXinController : Controller
    {
        private readonly ILogger _logger;
        private readonly IRepository<Comment, int> _commentRepostitory;
        private readonly IRepository<User, int> _userRepostitory;
        private readonly IRepository<ProductPic, int> _productPicRepostitory;
        private readonly IRepository<Product, int> _productRepostitory;
        private readonly IRepository<UserAddress, int> _addressRepostitory;

        public YongHuZhongXinController(ILogger<YongHuZhongXinController> logger
            , IRepository<Comment, int> commentRepostitory,
            IRepository<User, int> userRepostitory,
            IRepository<ProductPic, int> productPicRepostitory,
            IRepository<Product, int> productRepostitory,
            IRepository<UserAddress,int> addressRepostitory)
        {
            _logger = logger;
            _commentRepostitory = commentRepostitory;
            _userRepostitory = userRepostitory;
            _productPicRepostitory = productPicRepostitory;
            _productRepostitory = productRepostitory;
            _addressRepostitory= addressRepostitory;
        }
        public IActionResult Index()
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                //这里通过 HttpContext.User.Claims 可以将我们在Login这个Action中存储到cookie中的所有
                //claims键值对都读出来，比如我们刚才定义的UserName的值Wangdacui就在这里读取出来了
                var userid = HttpContext.User.Claims.First().ValueType;//获取cookie存储的用户id
                var id = int.Parse(userid);
                var total = _userRepostitory.GetAllList().Where(x => x.Id == id).FirstOrDefault();
                var userName = HttpContext.User.Claims.First().Type;//获取cookie存储的用户名
                var pingjia = _commentRepostitory.GetAll().Where(x=>x.Name==userName);
                ViewBag.Pingjia = pingjia.Count();
                return View(total);
            }
            else
            {
                return Redirect("/login/Login");
            }
        }

        #region 个人信息修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public IActionResult XinXiEdit(int id)
        {
            var userName = HttpContext.User.Claims.First().ValueType;
            var userid = int.Parse(userName);
            var user = _userRepostitory.GetAll();
            var s = user.FirstOrDefault(x => x.Id == userid);
            return View(s);
        }
        [HttpPost]
        public async Task<IActionResult> XinXiEdit(User user)
        {
            if (ModelState.IsValid)
            {
                await _userRepostitory.UpdateAsync(user);
                return RedirectToAction("index");
            }
            return View();
        }
        #endregion

        #region 修改状态是否为注销
        public async Task<IActionResult> ZhuXiao(int id)
        {
            var userName = HttpContext.User.Claims.First().ValueType;
            var userid = int.Parse(userName);
            var user = _userRepostitory.GetAll();
            var s = user.FirstOrDefault(x => x.Id == userid);
            return View(s);
        }
        [HttpPost]
        public async Task<IActionResult> ZhuXiao(User user)
        {
            if (ModelState.IsValid)
            {
                await _userRepostitory.UpdateAsync(user);
                return Redirect("/login/Login");
            }
            return View();
        }
        #endregion

        #region 账号管理
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public IActionResult ZhangHao(int id)
        {
            var userName = HttpContext.User.Claims.First().ValueType;
            var userid = int.Parse(userName);
            var user = _userRepostitory.GetAll();
            var s = user.FirstOrDefault(x => x.Id == userid);
            return View(s);
        }
        [HttpPost]
        public async Task<IActionResult> ZhangHao(User user)
        {
            if (ModelState.IsValid)
            {
                await _userRepostitory.UpdateAsync(user);
                return RedirectToAction("index");
            }
            return View();
        }
        #endregion

        #region 评价

        #region 评价页面
        public async Task<IActionResult> XiaoXi()
        {
            var userName = HttpContext.User.Claims.First().Type;
            //var comments = _commentRepostitory.GetAllList().Where(x => x.Name ==userName );
            var productPic = await _productPicRepostitory.GetAllListAsync();
            ViewBag.ProductPic = productPic;
            var comments = _commentRepostitory.GetAllList().Where(x=>x.Name==userName);

            return View(comments);
        }
        #endregion

        #region 评价删除
        public async Task<IActionResult> Del(int id)
        {
            Comment n = new Comment() { Id = id };
            if (ModelState.IsValid)
            {
                await _commentRepostitory.DeleteAsync(n);
                return RedirectToAction("Index");
            }
            return Content("删除失败");
        }
        #endregion

        #endregion

        #region 地址

        #region 地址列表
        public async Task<IActionResult> Address()
        {
            var userName = HttpContext.User.Claims.First().ValueType;
            var id = int.Parse(userName);
            var user = await _userRepostitory.GetAllListAsync();
            ViewBag.User = user;
            var address = _addressRepostitory.GetAllList().Where(x=>x.UserID==id);
            return View(address);
        }
        #endregion

        #region 地址添加
        /// <summary>
        /// 地址添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddressCreate()
        {
            var userName = HttpContext.User.Claims.First().ValueType;
            var id = int.Parse(userName);
            var user = _userRepostitory.GetAll().Where(x=>x.Id==id).FirstOrDefault();
            ViewBag.User = user;
            return View();
        }
        [HttpPost]
        public IActionResult AddressCreate(UserAddress address)
        {
            if (ModelState.IsValid)
            {
                _addressRepostitory.Insert(address);
                return RedirectToAction("Address");
            }
            return View();
        }
        #endregion

        #region 地址修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public IActionResult AddressEdit(int id)
        {
            var userName = HttpContext.User.Claims.First().ValueType;
            var userid = int.Parse(userName);
            var user = _userRepostitory.GetAll();
            var s = user.FirstOrDefault(x => x.Id == userid);
            ViewBag.User = s;
            var address=_addressRepostitory.GetAll().FirstOrDefault(x => x.Id == id);
            return View(address);
        }
        [HttpPost]
        public async Task<IActionResult> AddressEdit(UserAddress address)
        {
            if (ModelState.IsValid)
            {
                await _addressRepostitory.UpdateAsync(address);
                return RedirectToAction("Address");
            }
            return View();
        }
        #endregion

        #region 地址删除
        public async Task<IActionResult> AddressDel(int id)
        {
            UserAddress n = new UserAddress() { Id = id };
            if (ModelState.IsValid)
            {
                await _addressRepostitory.DeleteAsync(n);
                return RedirectToAction("Address");
            }
            return Content("删除失败");
        }
        #endregion

        #endregion
    }
}
